<template>
  <div class="hello">
    <input @input="handleInput" ref="input" type="text" v-bind="attrs"/>
<!--    <el-input type="text" v-bind="attrs"/>-->
    <h1>{{ msg }}</h1>
  </div>
</template>

<script lang="ts">
import { ref, defineComponent, nextTick, Ref, computed, watch } from 'vue'

export default defineComponent({
  name: 'helloWorld',
  inheritAttrs: false,
  props: {
    msg: String,
    modelValue: {
      type: [String, Number],
      default: ''
    }
  },
  setup (props, context) {
    const input = ref<HTMLDataElement | null>(null)
    const attrs = context.attrs
    const nativeInputValue = computed(() => (props.modelValue === null || props.modelValue === undefined) ? '' : String(props.modelValue))
    const setInput = (el: Ref<HTMLDataElement>, value: string) => {
      const input = el.value
      if (!input || input.value === nativeInputValue.value) return
      input.value = value
    }
    const handleInput = (event: Event) => {
      const { value } = event.target as HTMLInputElement
      // console.info(val)
      context.emit('update:modelValue', value)
      context.emit('input', value)
    }
    const setNativeInputValue = () => {
      setInput(input as Ref<HTMLDataElement>, props.modelValue as string)
    }
    watch(nativeInputValue, () => {
      setNativeInputValue()
    })
    nextTick(() => {
      setNativeInputValue()
      console.info(input)
    })
    return {
      attrs,
      input,
      handleInput
    }
  }
})
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
